package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.WmsInWarehous;
import com.ruoyi.business.domain.WmsInWarehousDetail;
import com.ruoyi.business.mapper.WmsInWarehousDetailMapper;
import com.ruoyi.business.mapper.WmsInWarehousMapper;
import com.ruoyi.business.service.IWmsInWarehousService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 入库单记录Service业务层处理
 * 
 * @author sz
 * @date 2024-01-08
 */
@Service
public class WmsInWarehousServiceImpl implements IWmsInWarehousService
{
    @Autowired
    private WmsInWarehousMapper wmsInWarehousMapper;
    @Autowired
    private WmsInWarehousDetailMapper wmsInWarehousDetailMapper;

    /**
     * 查询入库单记录
     * 
     * @param id 入库单记录ID
     * @return 入库单记录
     */
    @Override
    public WmsInWarehous selectWmsInWarehousById(Long id)
    {
        return wmsInWarehousMapper.selectWmsInWarehousById(id);
    }

    /**
     * 查询入库单记录列表
     * 
     * @param wmsInWarehous 入库单记录
     * @return 入库单记录
     */
    @Override
    public List<WmsInWarehous> selectWmsInWarehousList(WmsInWarehous wmsInWarehous)
    {
        return wmsInWarehousMapper.selectWmsInWarehousList(wmsInWarehous);
    }

    /**
     * 新增入库单记录
     * 
     * @param wmsInWarehous 入库单记录
     * @return 结果
     */
    @Override
    public int insertWmsInWarehous(WmsInWarehous wmsInWarehous)
    {
        wmsInWarehous.setCreateTime(DateUtils.getNowDate());
        return wmsInWarehousMapper.insertWmsInWarehous(wmsInWarehous);
    }

    /**
     * 修改入库单记录
     * 
     * @param wmsInWarehous 入库单记录
     * @return 结果
     */
    @Override
    public int updateWmsInWarehous(WmsInWarehous wmsInWarehous)
    {
        return wmsInWarehousMapper.updateWmsInWarehous(wmsInWarehous);
    }

    /**
     * 删除入库单记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWmsInWarehousByIds(String ids)
    {
        return wmsInWarehousMapper.deleteWmsInWarehousByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除入库单记录信息
     * 
     * @param id 入库单记录ID
     * @return 结果
     */
    @Override
    public int deleteWmsInWarehousById(Long id)
    {
        return wmsInWarehousMapper.deleteWmsInWarehousById(id);
    }

    @Override
    public int deleteWmsInWarehousByPlanId(Long planId) {
        WmsInWarehous wmsInWarehous = new WmsInWarehous();
        wmsInWarehous.setInPlanId(planId);
        List<WmsInWarehous> list = wmsInWarehousMapper.selectWmsInWarehousList(wmsInWarehous);
        if(!list.isEmpty()){
            wmsInWarehousMapper.deleteWmsInWarehousById(list.get(0).getId());
            //查询入库单关联的明细
            WmsInWarehousDetail wmsInWarehousDetail = new WmsInWarehousDetail();
            wmsInWarehousDetail.setInWarehousId(list.get(0).getId());
            List<WmsInWarehousDetail> detailList = wmsInWarehousDetailMapper.selectWmsInWarehousDetailList(wmsInWarehousDetail);
            for(WmsInWarehousDetail obj : detailList){
                wmsInWarehousDetailMapper.deleteWmsInWarehousDetailById(obj.getId());
            }
        }
        return 1;
    }

    /**
     * 出入库统计
     * @param info
     * @return
     */
    @Override
    public List<Map<String, Object>> statInOut(WmsInWarehous info) {
        List<Map<String, Object>> list = null;
        if(info.getStatType() == 1){
            //近七天
            Map<String,Object> params = new HashMap<>();
            params.put("endTime",DateUtils.parseDateToStr("yyyy-MM-dd",DateUtils.addDays(new Date(),-6)));
            list = wmsInWarehousMapper.statNumBy7Day(params);
            List<Map<String,Object>> dataList = getMonthDateList(DateUtils.addDays(new Date(),-6),new Date());
            for(Map<String,Object> map1 : dataList){
                for(Map<String,Object> map2 : list){
                    if(map1.get("date").equals(map2.get("date"))){
                        map1.put("num",map2.get("num"));
                    }
                }
            }
            list = dataList;
        }else if(info.getStatType() == 2){
            //本月
            Map<String,Object> params = new HashMap<>();
            params.put("endTime",DateUtils.parseDateToStr("yyyy-MM",new Date()));
            list = wmsInWarehousMapper.statNumByNowMonth(params);
            String time1 = DateUtils.parseDateToStr("yyyy-MM-dd",DateUtils.getMonthFirstDay());
            String time2 = DateUtils.parseDateToStr("yyyy-MM-dd",DateUtils.getMonthLastDay());
            List<Map<String,Object>> dataList = getMonthDateList(DateUtils.dateTime("yyyy-MM-dd",time1),DateUtils.dateTime("yyyy-MM-dd",time2));
            for(Map<String,Object> map1 : dataList){
                for(Map<String,Object> map2 : list){
                    if(map1.get("date").equals(map2.get("date"))){
                        map1.put("num",map2.get("num"));
                    }
                }
            }
            list = dataList;
        }else if(info.getStatType() == 3 || info.getStatType() == 4){
            //本年 或者自定义
            Map<String,Object> params = new HashMap<>();
            params.put("endTime",DateUtils.parseDateToStr("yyyy",new Date()));
            list = wmsInWarehousMapper.statNumByNowYear(params);

            List<Map<String,Object>> dataList = getYearDateList(DateUtils.parseDateToStr("yyyy",new Date()));
            for(Map<String,Object> map1 : dataList){
                for(Map<String,Object> map2 : list){
                    if(map1.get("date").equals(map2.get("date"))){
                        map1.put("num",map2.get("num"));
                    }
                }
            }
            list = dataList;
        }
        return list;
    }

    /**
     * 根据入库计划单号，查询数据
     * @param planSn
     * @return
     */
    @Override
    public WmsInWarehous getWarehousByPlanSn(String planSn) {
        return wmsInWarehousMapper.getWarehousByPlanSn(planSn);
    }

    public List<Map<String,Object>> getMonthDateList(Date startTime,Date endTime){
        String time2 = DateUtils.parseDateToStr("yyyy-MM-dd",endTime);
        List<Map<String,Object>> dateList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("date",DateUtils.parseDateToStr("yyyy-MM-dd",startTime));
        map.put("num",0);
        dateList.add(map);
        for(int i=1;i<=3650;i++){
            Date nowDate = DateUtils.addDays(startTime,i);
            String time1 = DateUtils.parseDateToStr("yyyy-MM-dd",nowDate);
            if(nowDate.getTime() <= endTime.getTime()){
                Map<String,Object> m = new HashMap<>();
                m.put("date",time1);
                m.put("num",0);
                dateList.add(m);
            }else{
                break;
            }
        }
        return dateList;
    }

    public List<Map<String,Object>> getYearDateList(String year){
        List<Map<String,Object>> dateList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("date",year+"-01");
        map.put("num",0);
        dateList.add(map);
        for(int i=2;i<=12;i++){
            if(i<10){
                Map<String,Object> map1 = new HashMap<>();
                map1.put("date",year+"-0"+i);
                map1.put("num",0);
                dateList.add(map1);
            }else{
                Map<String,Object> map1 = new HashMap<>();
                map1.put("date",year+"-"+i);
                map1.put("num",0);
                dateList.add(map1);
            }
        }
        return dateList;
    }
}
